import re

Unicom = ("130"   ,"131"   ,"132"   ,"145"   ,"155"   ,"156"   ,"185"   ,"186")
Mobile = ("134"   ,"135"   ,"136"   ,"137"   ,"138"   ,"139"   ,"147"   ,"150"   ,"151"   ,"152"   ,"157"   ,"158"   ,"159"   ,"178"   ,"182"   ,"183"   ,"187"   ,"188")
TeleCom = ("133"   ,"149"   ,"153"   ,"173"   ,"177"   ,"180"   ,"181"   ,"189")
def get_operate(number):
    for un in Unicom:
        if number.startswith(un):
            return "Unicom"
    for mo in Mobile:
        if number.startswith(mo):
            return "Mobile"
    for te in TeleCom:
        if number.startswith(te):
            return "TeleCom"
    return ""
def save_content(num_list):
   file_unicom = "unicom.txt"
   file_mobile = "mobile.txt"
   file_telecom = "telecom.txt"
   txt_unicom = "联通\r\n"
   txt_mobile = "移动\r\n"
   txt_telecom = "电信\r\n"
   for item in num_list:
       str1 = "姓名："+item["name"]+"   电话号码："+item["number"]+"\r\n"
       if item["operate"] == "Unicom":
           txt_unicom = txt_telecom+str1
       elif item["operate"] == "Mobile":
           txt_mobile = txt_mobile+str1
       else:
           txt_telecom = txt_telecom+str1

   with open(file_unicom, 'w',encoding = 'utf-8') as file_object:
       file_object.write(txt_unicom)
   with open(file_mobile, 'w',encoding = 'utf-8') as file_object:
       file_object.write(txt_mobile)
   with open(file_telecom, 'w',encoding = 'utf-8') as file_object:
       file_object.write(txt_telecom)
   print(num_list)

def input_num():
    #电话列表
    number_list = []
    #电话判重
    num_set = set()
    while True:
        #输入
        in_str_name = input("请输入姓名：")
        if in_str_name =="#":
            break
        in_str_number = input("请输入号码：")
        if in_str_name =="#":
            break
        #验证电话号码
        # 判断错误！
        ret = re.match(r"^1[35678]\d{9}$", in_str_number)
        if ret:
            operate = get_operate(in_str_number)
            name_number = {"name":in_str_name,"number":in_str_number,"operate":operate}
            if not (num_set.__contains__(in_str_number)):
                num_set.add(in_str_number)
                number_list.append(name_number)
            else:
                print("电话号码重复")
        else:
            print("您输入号码格式错误")
    return number_list
def main():
    save_content(input_num())
if __name__ == '__main__':
    main()